//
//  XXPickerView.swift
//  
//
//  Created by lxf on 2022/10/25.
//  
//

import UIKit

open class XXBasePickerView: UIPickerView {

    public  var dataGroup:[[AnyObject]] = []
    
    public  var dataSelect:[AnyObject?]{
        var selectItem = [AnyObject?].init()
        for i in 0..<dataGroup.count {
            let selectRow = selectedRow(inComponent: i)
            selectItem.append(rowDataBy(component: i, row: selectRow))
        }
        return selectItem
    }
    
    private var callback:((XXBasePickerView,[AnyObject?])->())?
    
    public func setOnValueChange(_ callback:@escaping (XXBasePickerView,[AnyObject?])->()){
        self.callback = callback
    }
    
    

    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        initView()
    }
    
    public init() {
        super.init(frame: CGRect.init())
        initView()
    }
    
    open  func initView() {
        self.delegate = self
        self.dataSource = self
        
    }
    
    
    // MARK: -
    public var separator = ":"
    public var separatorColor = UIColor.white
    public var separatorEnable = true
    // 需要手动调用 一次
    // setUpSeparator()
    
    open var separatorViews = [UILabel].init()
    open func setUpSeparator(){
        for view in separatorViews {
            view.removeFromSuperview()
        }
        separatorViews.removeAll()
        
        let componentCount = numberOfComponents(in: self)-1
        guard componentCount > 0 else {
            return
        }
        if separatorEnable == false{
            return
        }
        for i in 0..<componentCount {
            let v = UILabel.init()
            v.textColor = separatorColor
            v.textAlignment = .center
            v.tag = i + 10000
            v.text = separator
            separatorViews.append(v)
            self.addSubview(v)
        }
        
    }
    
    open override func reloadAllComponents() {
        super.reloadAllComponents()
        setUpSeparator()
    }
    open override func reloadComponent(_ component: Int) {
        super.reloadComponent(component)
        setUpSeparator()
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        
        let separatorViewsCount = separatorViews.count
        if separatorEnable == false || separatorViewsCount <= 0{
            return
        }
        
        let splitCount:CGFloat = CGFloat(separatorViewsCount+1)
        
        let oneW = self.frame.width / splitCount
//        print("oneW \(oneW)")
        
            
        let centerY = self.frame.height * 0.5
//        print("centerY \(centerY)")
        
        for i in 0..<separatorViewsCount {
            let view = separatorViews[i]
            view.sizeToFit()
            view.center = CGPoint.init(x: oneW * CGFloat(i+1), y: centerY)
        }
        
    }
    
    open func groupBy(component: Int)->[AnyObject]{
        return dataGroup[component]
    }
    open func rowDataBy(component: Int,row: Int)->AnyObject?{
        return dataGroup[component].safe(index: row)
    }

}
extension XXBasePickerView:UIPickerViewDelegate{
    open func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.callback?(self,dataSelect)
    }
}

extension XXBasePickerView:UIPickerViewDataSource{
    
    open  func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return dataGroup.count
    }
    
    
    open  func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataGroup[component].count
    }
    
    
}

open class TitlePickerView: XXBasePickerView {
    open func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return rowDataBy(component: component, row: row)?.description
    }
}

open class AttributedTitlePickerView: XXBasePickerView {
    
    private var makeAttributedStringFunc:((_ pickerView:AttributedTitlePickerView,_ data:AnyObject?,_ row: Int, _ component: Int)->NSAttributedString)?
    
    open func setMakeAttributedStringFunc(_ makeAttributedStringFunc: ((_ pickerView:AttributedTitlePickerView,_ data:AnyObject?,_ row: Int, _ component: Int)->NSAttributedString)?){
        self.makeAttributedStringFunc = makeAttributedStringFunc
    }
    
    
    open func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let data = rowDataBy(component: component, row: row)
        
        return makeAttributedStringFunc?(self,data,row,component)
    }
}

public typealias Type_decorateTitleLabelFunc = ((LabelTitlePickerView,UILabel, AnyObject?,_ row:Int,_ component:Int)->())
open class LabelTitlePickerView: XXBasePickerView {
    
    open var textColor:UIColor = .white
    open var textSize:UIFont = UIFont.systemFont(ofSize: 20)
    
    private var decorateTitleLabelFunc:Type_decorateTitleLabelFunc?
    
    open func setDecorateTitleLabelFunc(_ decorateTitleLabelFunc: Type_decorateTitleLabelFunc?){
        self.decorateTitleLabelFunc = decorateTitleLabelFunc
    }
    
    open func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        var label:UILabel? = view as? UILabel
        
        if label == nil {
            label = UILabel.init()
        }
        guard let label = label else { return UIView.init() }
        
        label.textAlignment = .center
        
        
        let data = rowDataBy(component: component, row: row)
        let title = data?.description ?? ""
        label.text = title
        label.textColor = textColor
        label.font = textSize
        
        
        if let decorateTitleLabelFunc = decorateTitleLabelFunc{
            decorateTitleLabelFunc(self,label,data,row,component)
        }else{
            
        }
        
        
        return label
    }
    
}

public protocol ModelPickerViewItemData:NSObjectProtocol{
    func getLabel()->String
    func getValue()->Any
}

open class ModelPickerView: XXBasePickerView {
    
    open var textColor:UIColor = .white
    open var textSize:UIFont = UIFont.systemFont(ofSize: 20)
    open var modeListGroup:[[ModelPickerViewItemData]]{
        set{
            self.dataGroup = newValue
        }
        get{
            return dataGroup as! [[ModelPickerViewItemData]]
        }
    }
    
    
    public override init() {
        super.init()
        self.dataGroup = []
    }
    public init(dataGroup:[[ModelPickerView]]) {
        super.init()
        self.dataGroup = dataGroup
    }
    
    public init(dataList:[ModelPickerView]) {
        super.init()
        self.dataGroup = [dataList]
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    open func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        var label:UILabel? = view as? UILabel
        
        if label == nil {
            label = UILabel.init()
        }
        guard let label = label else { return UIView.init() }
        
        label.textAlignment = .center
        
        
        let data = rowDataBy(component: component, row: row) as! ModelPickerViewItemData
        let title = data.getLabel()
        label.text = title
        label.textColor = textColor
        label.font = textSize
        
        return label
    }
    
    
    public  var dataSelectModel:[ModelPickerViewItemData?]{
        var selectItem = [ModelPickerViewItemData?].init()
        for i in 0..<dataGroup.count {
            let rowDateByComponent = dataSelectModel(by: i)
            selectItem.append(rowDateByComponent)
        }
        return selectItem
    }
    public  func dataSelectModel(by component:Int)->ModelPickerViewItemData?{
        let selectRow = selectedRow(inComponent: component)
        if -1 == selectRow {
            return nil
        }
        let rowDateByComponent = rowDataBy(component: component, row: selectRow)
        return rowDateByComponent as? ModelPickerViewItemData
    }
    
 
    private var callbackByModel:((ModelPickerView,Int,ModelPickerViewItemData?)->())?
    public func setOnModelValueChange(_ _callback:@escaping (ModelPickerView,Int,ModelPickerViewItemData?)->()){
        self.callbackByModel = _callback
    }
    
    override
    open func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        super.pickerView(pickerView, didSelectRow: row, inComponent: component)
        let rowDataByComponent = rowDataBy(component: component, row: row) as? ModelPickerViewItemData
        self.callbackByModel?(self,component,rowDataByComponent)
        self.callbackByModelList?(self,dataSelectModel)
    }
    
    private var callbackByModelList:((ModelPickerView,[ModelPickerViewItemData?])->())?

    public func setOnModelListValueChange(_ _callback:@escaping (ModelPickerView,[ModelPickerViewItemData?])->()){
        self.callbackByModelList = _callback
    }
}


